package com.roncoo.permission.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.roncoo.permission.entity.Admin;
import com.roncoo.permission.entity.Message;
import com.roncoo.permission.service.IAdminService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yuechuanhao
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private IAdminService iAdminService;

    /**
     * 登录
     * @param admin
     * @return
     */
    @PostMapping("blogin")
    public Message blogin(Admin admin){
        System.out.println("controller====="+admin.getPassworld()+admin.getPhone());
        return iAdminService.bLogin(admin);
    }

    /**
     * 分页 模糊 查询
     */
    @PostMapping("findAllPage")
    public IPage findAllPage(Admin admin, Page page) {
        IPage Page = iAdminService.findAllPage(admin, page);
        return Page;
    }

    /**
     * 添加 手机号 名称 备注
     */
    @PostMapping("add")
    public boolean add(Admin admin) {
        String pwd = "123456";
        admin.setPassworld(new Md5Hash(pwd).toString());
        return iAdminService.save(admin);
    }

    /**
     * 通过id删除
     *
     * @param
     * @return
     */
    @PostMapping("delById")
    public boolean delById( Admin admin ) {
        return iAdminService.removeById(admin.getId());
    }

    /**
     * 通过id修改 名称 备注 密码
     */
    @PostMapping("updName")
    public boolean updName( Admin admin ) {
        String md5Hash = new Md5Hash(admin.getPassworld()).toString();
        admin.setPassworld(md5Hash);
        return iAdminService.updateById(admin);
    }

    /**
     * 通过id修改 权限
     */
    @PostMapping("updataRole")
    public boolean updataRole(Admin admin) {
        System.out.println(admin.getId()+"========"+admin.getAdminroleid());
        return iAdminService.updateById(admin);
    }

    /**
     *  修该 状态
     * @param admin
     * @return
     */
    @PostMapping("upState")
    public boolean upState(Admin admin){
        return iAdminService.updateById(admin);
    }

    /**
     * 查询此角色是否有人
     */
    @GetMapping("findPeople")
    public boolean findPeople(Integer roleid){
        return iAdminService.findPeople(roleid);
    }


    @GetMapping("findrole")
    public boolean findrole(Integer id){
        return iAdminService.findrole(id);
    }


}

